<template>
  <div class="app-container">
    <!-- 查询-->
    <div class="query">
      <el-form :inline="true" :model="queryparam" class="demo-form-inline">
        <el-form-item label="名称">
          <el-input v-model="queryparam.rolename" placeholder="名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="fetchData">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addOpen">添加</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!--表格-->
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column prop="rolename" label="名称"></el-table-column>
      <el-table-column prop="flag" label="状态" :formatter="flagType"></el-table-column>
      <el-table-column prop="roledesc" label="描述"></el-table-column>
      <el-table-column label="操作" width="250px">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="openMod(scope.row)">修改</el-button>
          <el-button size="mini" type="primary" @click="openTrade(scope.row)">交易权限</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-size="pagesize"
      :page-sizes="[5,10,50,100]"
      layout="total, sizes,prev, pager, next"
      :total="total"
    ></el-pagination>

    <el-dialog title="添加角色" :visible.sync="addVisible">
      <el-form :model="addparam" :rules="addroles" ref="addfrom">
        <el-form-item label="名    称" :label-width="formLabelWidth" prop="rolename">
          <el-input v-model="addparam.rolename" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="状    态" :label-width="formLabelWidth" prop="flag">
          <el-radio-group v-model="addparam.flag">
            <el-radio label="0">有效</el-radio>
            <el-radio label="1">无效</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="描    述" :label-width="formLabelWidth" prop="roledesc">
          <el-input v-model="addparam.roledesc" type="textarea"></el-input>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="菜 单" :label-width="formLabelWidth">
              <el-tree
                :data="allmenus"
                ref="tree"
                show-checkbox
                node-key="id"
                :props="defaultProps"
              ></el-tree>
            </el-form-item>
          </el-col>
          <el-col :span="12"></el-col>
        </el-row>
      </el-form>

      <div slot="footer">
        <el-button @click="closeAdd">取 消</el-button>
        <el-button type="primary" :disabled="isDisable" @click="addrole">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 修改角色-->
    <el-dialog title="修改角色" :visible.sync="modVisible">
      <el-form :model="modparam" :rules="addroles" ref="modfrom">
        <el-form-item label="名    称" :label-width="formLabelWidth">
          <el-input v-model="modparam.rolename" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="状    态" :label-width="formLabelWidth">
          <el-radio-group v-model="modparam.flag">
            <el-radio label="0">有效</el-radio>
            <el-radio label="1">无效</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="描    述" :label-width="formLabelWidth">
          <el-input v-model="modparam.roledesc" type="textarea"></el-input>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="菜 单" :label-width="formLabelWidth">
              <el-tree
                :data="allmenus"
                ref="modtree"
                show-checkbox
                :default-checked-keys="modparam.menus"
                node-key="id"
                :props="defaultProps"
              ></el-tree>
            </el-form-item>
          </el-col>
          <el-col :span="12"></el-col>
        </el-row>
      </el-form>

      <div slot="footer">
        <el-button @click="closeMod">取 消</el-button>
        <el-button type="primary" :disabled="isDisable" @click="modRole">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="交易权限" :visible.sync="tradeVisible">
      <el-form :inline="true" :model="tradeparam" class="demo-form-inline">
        <el-select
          v-model="tradeparam.trades"
          multiple
          collapse-tags
          filterable
          style="margin-left: 20px;"
          placeholder="请选择"
        >
          <el-option
            v-for="item in unowntrades"
            :key="item.id"
            :label="item.tradename"
            :value="item.id"
          ></el-option>
        </el-select>
        <el-form-item>
          <el-button type="primary" @click="addTrade">添加</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="owntrades"
        element-loading-text="Loading"
        height="350px"
        border
        fit
        highlight-current-row
      >
        <el-table-column property="tradename" label="名称"></el-table-column>
        <el-table-column property="tradecode" label="交易码"></el-table-column>
        <el-table-column property="flag" label="状态" :formatter="flagType"></el-table-column>

        <el-table-column label="操作" width="100px">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="danger"
              :disabled="isDisable"
              @click="tradeDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>


</template>

<script>
import {
  deltrade,
  addtrades,
  unowntrade,
  owntrade,
  mod,
  queryid,
  add,
  del,
  list
} from "@/api/role";
import { menutree } from "@/api/menu";
//公共变量方法 混入
import basemixni from "@/mixins/basemixin";

export default {
  mixins: [basemixni],
  data() {
    return {
      queryparam: {
        pagesize: 5,
        pagenum: 1,
        rolename: ""
      },
      //添加数据定义
      addparam: {
        rolename: "",
        flag: "0",
        roledesc: "",
        menus: []
      },
      addroles: {
        rolename: [{ required: true, message: "输入名称", trigger: "blur" }],
        roledesc: [{ required: true, message: "输入描述", trigger: "blur" }]
      },
      allmenus: [],
      defaultProps: {
        children: "children",
        label: "menuname"
      },
      //修改
      modparam: {
        id: "",
        rolename: "",
        flag: "0",
        roledesc: "",
        menus: []
      },
      tradeVisible: false,
      owntrades: [],
      unowntrades: [],
      tradeparam: {
        roleid: "",
        trades: []
      }
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    //属性数据
    fetchData() {
      this.listLoading = true;
      list(this.queryparam).then(response => {
        this.list = response.data.list;
        this.total = response.data.totalsize;
        this.listLoading = false;
      });
    },
    addOpen() {
      this.addVisible = true;

      menutree().then(response => {
        this.allmenus = response.data;
      });
    },
    addrole() {
      this.$refs.addfrom.validate(valid => {
        if (valid) {
          this.addparam.menus = this.$refs.tree.getCheckedKeys();
          this.isDisable = true;
          add(this.addparam).then(response => {
            this.isDisable = false;
            this.fetchData();
            this.closeAdd();
          });
        } else {
          return false;
        }
      });
    },
    //修改角色
    openMod(row) {
      menutree().then(response => {
        this.allmenus = response.data;
        queryid({ id: row.id }).then(response => {
          this.modparam.menus = response.data.menus;
          this.modparam.id = response.data.id;
          this.modparam.rolename = response.data.rolename;
          this.modparam.roledesc = response.data.roledesc;
          this.modVisible = true;
        });
      });
    },
    modRole() {
      this.$refs.modfrom.validate(valid => {
        if (valid) {
          this.modparam.menus = this.$refs.modtree.getCheckedKeys();
          this.isDisable = true;

          mod(this.modparam).then(response => {
            this.isDisable = false;

            this.fetchData();
            this.closeMod();
          });
        } else {
          return false;
        }
      });
    },

    //删除
    handleDelete(row) {
      this.$confirm("确定删除角色" + row.rolename, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          del({ id: row.id }).then(response => {
            this.fetchData();
            this.$message({
              type: "success",
              message: "删除成功!"
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    fetchTrade(roleid) {
      owntrade({ id: roleid }).then(response => {
        this.owntrades = response.data;
        this.tradeparam.roleid = roleid;
        unowntrade({ id: roleid }).then(response => {
          this.unowntrades = response.data;
        });
      });
    },
    openTrade(row) {
      this.fetchTrade(row.id);
      this.tradeVisible = true;
    },
    tradeDelete(row) {
      this.$confirm("确定删除" + row.tradename, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deltrade({ roleid: this.tradeparam.roleid, tradeid: row.id }).then(
            response => {
              this.fetchTrade(this.tradeparam.roleid);
              this.$message({
                type: "success",
                message: "删除成功!"
              });
            }
          );
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    addTrade() {
      this.isDisable = false;
      addtrades(this.tradeparam).then(response => {
        this.isDisable = false;
        this.tradeparam.trades = [];
        this.fetchTrade(this.tradeparam.roleid);
      });
    }
  }
};
</script>

<style lang="scss">
.query {
  margin-top: 10px;
  float: right;
}
</style>